package tw.com.diray;

import tw.com.diary.api.domain.ModifyPasswordDTO;
import tw.com.diary.api.service.ModifyPasswordService;
import tw.com.diary.core.ModifyPasswordServiceImpl;
import tw.com.diray.comm.ui.DirToast;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class ModifyPassword extends Activity {

	private EditText oldPassword = null;
	private EditText newPassword = null;
	private EditText newPassword2 = null;

	private ModifyPasswordDTO dto = new ModifyPasswordDTO();
	private ModifyPasswordService modifyPasswordService = null;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_password);
		
		this.modifyPasswordService=ModifyPasswordServiceImpl.intance(this);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		this.oldPassword = (EditText) this.findViewById(R.id.oldText);
		this.newPassword = (EditText) this.findViewById(R.id.newText);
		this.newPassword2 = (EditText) this.findViewById(R.id.newPassword2);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.modify_password, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		int id = item.getItemId();

		switch (id) {
		case R.id.action_sumit:

			try {
				this.dto.setNewPassword(this.newPassword.getText().toString());
				this.dto.setNewPassword2(this.newPassword2.getText().toString());
				this.dto.setOldPassword(this.oldPassword.getText().toString());

				this.modifyPasswordService.modifyPassword(dto);

				Toast.makeText(ModifyPassword.this, "密碼修改成功",
						Toast.LENGTH_SHORT).show();
				finish();

			} catch (Exception e) {
				DirToast.show(ModifyPassword.this, e);
			}

			
		}

		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
	}
}
